/**
 * Created by jh on 2016/9/22.
 */
$(function() {
    //当添加新选项卡时，如果选项卡超过ul的宽度，设置scrollLeft
    $('.mainTabs').tabs({
        fit: true,
        singlePages: false,
        scrolled: true,
        onAdd: function () {
            var pos = $('.mainTabs').find('>ul.nav').scrollLeft();
            var id = $(this).attr('id');
            var currentLI = $('a[href="#' + id + '"]').parent();
            $('.mainTabs').find('>ul.nav').animate({scrollLeft: pos + currentLI.width()}, 400);
            $(this).addClass('animated');
            //1s后移除animated样式
            var that = this;
            setTimeout(function () {
                $(that).removeClass('animated');
            }, 1000);
        }
    });
    

	/* 双击刷新TAB选项卡 */
    /*
	$(document).on('dblclick','.nav-tabs li.active',function(e){
		var obj = $(".page-tabs-content").children("[data-id].active");
    	var l = $('.J_iframe[data-id="' + $(obj).data("id") + '"]');
        var k = l.attr("src");
        l.attr('src',k);
	});
	*/
	
	var menu = new BootstrapMenu('.nav-tabs li.active', {
	      actions: [{
	        name: '刷新',
	        onClick: function() {
	        	var obj = $(".page-tabs-content").children("[data-id].active");
	        	var l = $('.J_iframe[data-id="' + $(obj).data("id") + '"]');
	            var k = l.attr("src");
	            l.attr('src',k);
	        }
	      }, {
	        name: '关闭所有选项卡',
	        onClick: function() {
	        	 $(".page-tabs-content").children("[data-id]").not(":first").each(function() {
	                 $('.J_iframe[data-id="' + $(this).data("id") + '"]').remove();
	                 $(this).remove();
	             });
	             $(".page-tabs-content").children("[data-id]:first").each(function() {
	                 $('.J_iframe[data-id="' + $(this).data("id") + '"]').show();
	                 $(this).addClass("active");
	             });
	             $(".page-tabs-content").css("margin-left", "0");
	        }
	      }, {
	        name: '关闭其他选项卡',
	        onClick: function() {
	        	$(".page-tabs-content").children("[data-id]").not(":first").not(".active").each(function() {
		              $('.J_iframe[data-id="' + $(this).data("id") + '"]').remove();
		              $(this).remove();
		          });
		          $(".page-tabs-content").css("margin-left", "0");
	        }
	      }]
	    });


    //左侧菜单导航
    // var data1 = [
    //     /*{id: "1", parentId: "0", title: "我的测试", iconClass: "fa fa-certificate", url: "myTest", orderNum: 1},*/
    // 	{id: "23", parentId: "0", title: "测试用例", iconClass: "fa fa-navicon", url: "testCase/testCase", orderNum: 1},
    //     {id: "2", parentId: "0", title: "测试报告", iconClass: "fa fa-industry", url: "testReport", orderNum: 2},
    //     {id: "3", parentId: "0", title: "测试工具", iconClass: "fa fa-asterisk", url: "", orderNum: 3},
    //     {id: "17", parentId: "0", title: "接口测试", iconClass: "fa fa-yelp", url: "", orderNum: 4},
    //     {id: "4", parentId: "0", title: "项目管理", iconClass: "fa fa-gg", url: "", orderNum: 5},
    //     {id: "5", parentId: "0", title: "资源管理", iconClass: "fa fa-gears", url: "", orderNum: 6},
    //     {id: "6", parentId: "0", title: "系统管理", iconClass: "fa fa-key", url: "", orderNum: 7},
    //
    //
    //
    //     {id: "7", parentId: "3", title: "静态分析", iconClass: "icon-circle", url: "manual/sonar", orderNum: 1},
    //     /*{id: "8", parentId: "3", title: "功能测试", iconClass: "icon-circle", url: "loading", orderNum: 2},*/
    //     {id: "9", parentId: "3", title: "Jmeter测试", iconClass: "icon-circle", url: "Jmeter/Jmeter", orderNum: 3},
    //
    //     {id: "11", parentId: "4", title: "项目配置", iconClass: "icon-circle", url: "project/project", orderNum: 1},
    //
    //     {id: "12", parentId: "5", title: "测试资源", iconClass: "icon-circle", url: "resource/testResource", orderNum: 1},
    //     {id: "13", parentId: "5", title: "JOB管理", iconClass: "icon-circle", url: "resource/jobResource", orderNum: 2},
    //     /*{id: "21", parentId: "5", title: "服务器监控", iconClass: "icon-circle", url: "resource/serverMonitor", orderNum: 3},*/
    //
    //     {id: "14", parentId: "6", title: "日志管理", iconClass: "icon-circle", url: "logRecord/logRecord", orderNum: 1},
    //     {id: "15", parentId: "6", title: "用户管理", iconClass: "icon-circle", url: "userAccount/userManager", orderNum: 2},
    //     {id: "16", parentId: "6", title: "系统设置", iconClass: "icon-circle", url: "configMessage/sysSet", orderNum: 3},
    //     {id: "20", parentId: "6", title: "接口参数规则", iconClass: "icon-circle", url: "mockRule/mockRuleMgmt", orderNum: 4},
    //
    //     {id: "18", parentId: "17", title: "模块管理", iconClass: "icon-circle", url: "module/moduleMgmt", orderNum: 1},
    //     {id: "19", parentId: "17", title: "接口管理", iconClass: "icon-circle", url: "testIntfc/toTestIntfc", orderNum: 2},
    //     {id: "24", parentId: "17", title: "功能测试", iconClass: "icon-circle", url: "webTestProcedure/toWebTestProcedure", orderNum: 3},
    // ];
    
    // var data2 = [
    //     /*{id: "1", parentId: "0", title: "我的测试", iconClass: "fa fa-certificate", url: "myTest", orderNum: 1},*/
    // 	{id: "23", parentId: "0", title: "测试用例", iconClass: "fa fa-navicon", url: "testCase/testCase", orderNum: 1},
    //     {id: "2", parentId: "0", title: "测试报告", iconClass: "fa fa-industry", url: "testReport", orderNum: 2},
    //     {id: "3", parentId: "0", title: "测试工具", iconClass: "fa fa-asterisk", url: "", orderNum: 3},
    //     {id: "17", parentId: "0", title: "接口测试", iconClass: "fa fa-yelp", url: "", orderNum: 4},
    //     {id: "4", parentId: "0", title: "项目管理", iconClass: "fa fa-gg", url: "", orderNum: 5},
    //     {id: "5", parentId: "0", title: "资源管理", iconClass: "fa fa-gears", url: "", orderNum: 6},
    //     {id: "6", parentId: "0", title: "系统管理", iconClass: "fa fa-key", url: "", orderNum: 7},
    //
    //
    //     {id: "7", parentId: "3", title: "静态分析", iconClass: "icon-circle", url: "manual/sonar", orderNum: 1},
    //     /*{id: "8", parentId: "3", title: "功能测试", iconClass: "icon-circle", url: "loading", orderNum: 2},*/
    //     {id: "9", parentId: "3", title: "Jmeter测试", iconClass: "icon-circle", url: "Jmeter/Jmeter", orderNum: 3},
    //
    //     {id: "11", parentId: "4", title: "项目配置", iconClass: "icon-circle", url: "project/project", orderNum: 1},
    //
    //     {id: "12", parentId: "5", title: "测试资源", iconClass: "icon-circle", url: "resource/testResource", orderNum: 1},
    //
    //     {id: "14", parentId: "6", title: "日志管理", iconClass: "icon-circle", url: "logRecord/logRecord", orderNum: 1},
    //     {id: "20", parentId: "6", title: "接口参数规则", iconClass: "icon-circle", url: "mockRule/mockRuleMgmt", orderNum: 4},
    //
    //     {id: "18", parentId: "17", title: "模块管理", iconClass: "icon-circle", url: "module/moduleMgmt", orderNum: 1},
    //     {id: "19", parentId: "17", title: "接口管理", iconClass: "icon-circle", url: "testIntfc/toTestIntfc", orderNum: 2},
    //     /*{id: "20", parentId: "17", title: "参数管理", iconClass: "icon-circle", url: "param/paramMgmt", orderNum: 3},*/
    // ];
    
    // $('#side-menu').metisMenu({  //普通用户-2 无权查看系统和用户数据
    //     data: userType == 2 ? data2 : data1,
    //     onSelect: function () {
    //         updateTabs($('.mainTabs'), $(this));
    //     }
    // });

    var menuWidth = $('.navbar-static-side').width();
    //展开菜单导航和折叠菜单导航事件
    $('.navbar-minimalize').click(function () {
        $("body.coodo-layout").toggleClass("mini-navbar");

        var west = $('body.coodo-layout>.coodo-layout-west');
        var center = $('body.coodo-layout>.coodo-layout-center');
        var newMenuW = 60;//$('.navbar-static-side').width();

        if($("body.coodo-layout").hasClass('mini-navbar')){
            $(this).addClass('navbar-minimalize-vertical')
            center.width(center.width() + (menuWidth - newMenuW));
            west.width(newMenuW);
        } else {
            $(this).removeClass('navbar-minimalize-vertical')
            center.width(center.width() - (menuWidth - newMenuW));
            west.width(menuWidth);
        }
    });

    /*$("#side-menu").on('click', '>li', function () {
        $("body.coodo-layout").hasClass("mini-navbar") && NavToggle()
    });*/

    // 更换皮肤
    $('.nav-banner-control :first > ul > li').click(function() {
        $("head [href^='skin']").attr('href', $(this).attr('value'));
    });
});

function updateTabs(target, li) {
    var opts = target.tabs('options');
    var param = {};
    var url = li.find('>a').attr('href');
    var title = $.trim(li.find('>a>.nav-label').text());

    if (target.tabs('exists', title)) {
        target.tabs('select', title);
        return false;
    }

    param.closable = true;
    param.title = title;
    if (url != null && url != '') {
    	//$('.tab-content').openMask({msg:'正在加载页面...'});
        if (opts.singlePages) {
            $.ajax({
                url: url,
                dataType: 'html',
                success: function (res) {
                    param.content = res;
                    target.tabs('add', param);
                    target.tabs('select', title);
                    $.parser.parse($(target.tabs('getTab', title)));
                    //$('.tab-content').closeMask();
                }
            });
        } else {
            param.content = url;
            target.tabs('add', param);
            target.tabs('select', title);
            $('.tab-content').closeMask();
        }
    }
}

function addNewTabs(target, url,title,flag) {
//	debugger
    var opts = target.tabs('options');
    var param = {};

    if (target.tabs('exists', title)) {
    	//flag=1为相同title时重新加载页面
    	if(flag == 1){
    		target.tabs('remove', title);
    	}else{
    		target.tabs('select', title);
            return false;
    	}
    }

    param.closable = true;
    param.title = title;
    if (url != null && url != '') {
    	//$('.tab-content').openMask({msg:'正在加载页面...'});
        if (opts.singlePages) {
            $.ajax({
                url: url,
                dataType: 'html',
                success: function (res) {
                    param.content = res;
                    target.tabs('add', param);
                    target.tabs('select', title);
                    $.parser.parse($(target.tabs('getTab', title)));
                    //$('.tab-content').closeMask();
                }
            });
        } else {
            param.content = url;
            target.tabs('add', param);
            //console.info(target);
            target.tabs('select', title);
            //$('.tab-content').closeMask();
        }
    }
}

function NavToggle() {
    $(".navbar-minimalize").trigger("click");
}